package com.pets.springboot_petshop.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.pets.springboot_petshop.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailServiceImpl implements EmailService {
    private Integer Code;
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public String sendemail(String email) {
        Code= RandomUtil.randomInt(100000,999999);
//        System.out.println("Generated Verification Code: " + Code);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(email);
        simpleMailMessage.setSubject("验证码");
        simpleMailMessage.setText("您的邮箱验证码为:"+Code.toString());

        javaMailSender.send(simpleMailMessage);
        return Code.toString();
    }
}
//        System.out.println("Sending email to: " + to + " with subject: " + Code);
// 发送邮件
/*        try {
            javaMailSender.send(simpleMailMessage);
            System.out.println("Email sent successfully to: " + to);
        } catch (Exception e) {
            System.out.println("Failed to send email to: " + to);
            e.printStackTrace();
        }*/